######################################################################
# Time-stamp: <05/08/11 16:13:26 ostolop>
#
# Routines to validate clustering queries
######################################################################
package EP::EPC::Interface::Validator::Hierarchical_Clustering;
use strict;

use EP::Config;

use EP::Common;
use EP::Common::DB;
use EP::Common::General;
use EP::Common::Error qw ( :try );

sub new {
  my $proto = shift;
  my ( $query ) = @_;
  my $class = ref($proto) || $proto;
  my $self  = {};
  bless $self, $class;

  $self->{query} = $query;
  return $self;
}

sub validate {
  my $self = shift;
  my ($component, $source) = @_;

  my $err_code = "";

  try {
#    checkNAs ( $component, $source );
  } catch EP::Common::Error with {
    my $e = shift;
    $err_code = "$e";
  };

  return $err_code;
}

sub checkNAs {
  my ( $component, $source ) = @_;

  my $src_dataset_id;
  my $cur_action_id = $component->findvalue ( 'section//submit[starts-with(@name, "current_action_id")]/value' );
  warn "Found $cur_action_id";
  if ( not $cur_action_id ) {
    $src_dataset_id = $component->findvalue ( 'section//submit[starts-with(@name, "current_root_dataset_id")]/value' );
  } else {
    $src_dataset_id = EP::Common::DB::get_action_dst_dataset ( $cur_action_id );
  }
  warn "Couldn't find dataset id..." if not $src_dataset_id;
  return "No source dataset was found!" if not $src_dataset_id;

  my $dataset = EP::Common::DB::get_dataset ( { id => $src_dataset_id} )->{0};
  my $hasNAs = `grep -m 1 -c NA $EP::Config::h->{EPNG_USRDATA_PATH}/$dataset->{folder}/$dataset->{filename}`;

  if ( $hasNAs =~ /1/ ) {
    throw EP::Common::Error ( -value => CLU_DATASET_HAS_NAS, -err_args => [ "this data" ] );
  }
}

1;
